﻿
Namespace Console

    Public Class MultiPlugConsole
        Inherits LibUtils.TL.Console.VirtualConsole


        Private _commandHandlers As System.Collections.Specialized.HybridDictionary

        Private _logger As Global.log4net.ILog

        Public Sub New(ByVal parent As System.Windows.Forms.Form)
            MyBase.new(parent)
            _logger = Global.log4net.LogManager.GetLogger(GetType(MultiPlugConsole))
            _commandHandlers = New System.Collections.Specialized.HybridDictionary
        End Sub


        Public Sub RegisterCommandHandler(ByVal commandHandler As IConsoleCommandHandler)
            ' nimmt einen neune commandhandler in die liste auf
            _commandHandlers.Add(commandHandler.Command, commandHandler)
        End Sub


        Protected Overrides Sub ExecuteCommand(ByVal command As String)

            ' den command am doppelpunkt aufteilen
            Dim splitted() As String = command.Split(CChar(":"))

            ' wenn der erste befehl ein registrierter befehl ist
            If _commandHandlers.Contains(splitted(0)) Then

                ' den commandhandler zwischenspeichern
                Dim currentCommand As IConsoleCommandHandler _
                    = CType(_commandHandlers(splitted(0)), IConsoleCommandHandler)

                Try

                    If _logger.IsDebugEnabled Then _
                        _logger.Debug("Execute ConsoleCommand: '" & command & "'")

                    ' den command ausführen
                    currentCommand.Execute(splitted)

                Catch ex As Exception

                    MyBase.WriteLine("ERROR: " & ex.Message)

                    If _logger.IsErrorEnabled Then _
                        _logger.Error(ex.Message, ex)

                End Try

            Else

                If _logger.IsDebugEnabled Then _
                    _logger.Debug("Execute ConsoleCommand form Base: '" & command & "'")


                ' den command über den vater ausführen
                MyBase.ExecuteCommand(command)

            End If

        End Sub

    End Class

End Namespace